package com.ruihui.activity.service.impl;

import com.ruihui.activity.pojo.Accoupon;
import com.ruihui.activity.pojo.Acgoods;
import com.ruihui.activity.pojo.Acgrecord;
import com.ruihui.activity.exceprion.ManualException;
import com.ruihui.activity.mapper.AccouponMapper;
import com.ruihui.activity.mapper.AcgoodsMapper;
import com.ruihui.activity.service.AccouponService;
import com.ruihui.activity.service.AcgoodsService;
import com.ruihui.activity.service.AcgrecordService;
import com.ruihui.activity.util.DateUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 用户优惠券表 服务实现类
 * </p>
 *
 * @author cf123
 * @since 2018-10-16
 */
@Service
public class AccouponServiceImpl extends ServiceImpl<AccouponMapper, Accoupon> implements AccouponService {
	@Autowired
	AccouponMapper accouponMapper;
	@Autowired
	AcgoodsMapper acgoodsMapper;
	@Autowired
	AcgoodsService acgoodsService;
	@Autowired
	AcgrecordService acgrecordService;
	public List<Accoupon> getAccoupons(Integer memberId,Integer state,Integer type,Integer id,Integer catid,BigDecimal acrdprice) {
		//state为空，赋值为零，防止类型比对错误
		if(StringUtils.isEmpty(state)){
			state=-1;
		}
		if(StringUtils.isEmpty(type)){
			type=-1;
		}
		Long nowDate=DateUtil.getDateline(); 
		List<Accoupon> list = accouponMapper
				.selectList(new EntityWrapper<Accoupon>().eq("userid", memberId).eq("del", 1)
						.eq(!StringUtils.isEmpty(id),"id", id)
						.eq(!StringUtils.isEmpty(catid)||catid.compareTo(-2)!=0,"cid", catid)
						.le(!StringUtils.isEmpty(acrdprice),"acrdprice", acrdprice)
						.where(state==0,"state = 1 and starttime <= {0} and endtime >= {0}", nowDate)
						.where(state==1, "state = 1 and starttime > {0}",nowDate)
						.where(state==2, "state = 2")
						.where(state==3, "endtime < {0} and state = 1",nowDate)
						.where(state==4, "endtime >= {0} and state=1",nowDate)
						.where(type==1, "type =1")
						.where(type==2, "type =2"));
		return list;
	}
	@Transactional
	public int getRedAccoupon(Integer id,Integer memberid ,Integer gnum) {
		int renum=1;
		Long nowDate=DateUtil.getDateline();
		//已领取优惠券
		int numed=0;
		//更新状态
		int code=0;
		Long nowdate=DateUtil.getDateline();
		Acgoods ac=this.iniacGoods(id);
		Accoupon accoupon=new Accoupon();
		if(!StringUtils.isEmpty(ac)){
			// 领取数量
			List<Accoupon> accoupons=accouponMapper.selectList(new EntityWrapper<Accoupon>().eq("userid", memberid)
					.eq("couponid", id).eq("del", 1).eq("state", 1).ge("endtime", nowDate));
			if(gnum.compareTo(ac.getAcnum())>0){
				throw new ManualException(-1, "优惠券不足");
			}
			if(!accoupons.isEmpty()&&accoupons.size()==1){
				accoupon=accoupons.get(0);
				numed=accoupon.getCnum().intValue();
				if((numed+gnum.intValue())>ac.getLimitbuy().intValue()){
					throw new ManualException(-1, "超过可领取数量");
				}
				accoupon.setCnum(numed+gnum.intValue());
				while (renum<=3&&code!=1) {
					code=accouponMapper.updateById(accoupon);
					renum++;
				}
				if(code!=1){
					throw new ManualException(-1, "领取失败");
				}
			}else{
				if(!StringUtils.isEmpty(ac.getCatid())){
					accoupon.setCid(ac.getCatid().intValue());
				}else{
					//通用
					accoupon.setCid(-1);
				}
				accoupon.setCnum(gnum);
				accoupon.setCouponid(ac.getId().intValue());
				accoupon.setCreatetime(new BigDecimal(nowdate));
				accoupon.setEndtime(ac.getEndtime());
				accoupon.setPod(ac.getAcprice());
				accoupon.setStarttime(ac.getStarttime());
				accoupon.setState(1);
				accoupon.setType(ac.getType());
				accoupon.setUserid(memberid);
				accoupon.setAcrdprice(ac.getAcrdprice());
				accoupon.setAcno(ac.getAcno());
				accoupon.setContents(ac.getContents());
				accoupon.setCoupname(ac.getGname());
				code=accouponMapper.insert(accoupon);
				if(code!=1){
					throw new ManualException(-1, "领取失败");
				}
			}
			//减库存，新增领取记录
			acgoodsService.acreduceNum(ac, gnum, id.longValue(), nowdate, memberid.longValue());
			
			
		}else{
			throw new ManualException(-1, "优惠券活动已过期");
		}
		return code;
	}
	
	public Acgoods iniacGoods(Integer id) {
		Long nowdate=DateUtil.getDateline();
		List<Acgoods> list = acgoodsMapper.selectList(new EntityWrapper<Acgoods>().eq("id", id)
				.eq("state", 1).le("starttime", nowdate).gt("endtime", nowdate));

		if (!list.isEmpty() && list.size() == 1) {
			return list.get(0);
		}
		return null;
	}


}
